<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
</head>

<body>
    <button onclick="showRoadState()">显示实时交通数据</button>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 10
            })
        })
        const style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: "green",
                width: 3
            })
        })
        var source = new ol.source.Vector({
            url: "http://39.103.151.139:8000/gis/road_state",
            format: new ol.format.GeoJSON()
        })
        var layer = new ol.layer.Vector({
            source,
            style
        })
        map.addLayer(layer);
        /* red  green  flow
        flow>1000 red
        flow<=1000 green
         */

        function showRoadState() {
            // console.log(source);
            source.forEachFeature(evt => {
                let color = null;
                var flow = evt.values_.flow;
                console.log(flow)
                if (flow > 1000) {
                    color = "red"
                } else {
                    color = "#398439"
                }
                let stateStyle = new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color,
                        width: 5
                    })
                })
                evt.setStyle(stateStyle);
            })
        }
    </script>
</body>

</html>